<template>
	<div class="certificate">
		<div class="add-btn" v-if="editState == 0" @click="editState = 1"><img src="@/assets/me/up.png"><span>添加</span></div>
		<div class="title">获得证书</div>
		<div class="certificate-content" v-if="editState == 0 && form.certificateInfos.length>0">
			<div class="certificate-item flex" v-for="(items,index) in form.certificateInfos" :key="index">
				<div class="label">{{items.year}}-{{items.month}}</div>
				<div class="line">
					<div class="dian"></div>
					<div class="line-box"></div>
				</div>
				<div class="item-val">{{items.certificateName}}</div>
				<div class="all-option transition-ease-in">
					<span class="all-option-edit" @click="editEducation(index)">编辑</span>
					<span class="all-option-del" @click="delOption(index)">删除</span>
				</div>
			</div>
		</div>
		<div class="certificate-content no-data" v-if="editState == 0 && form.certificateInfos.length==0">
			证书是您驰骋职场的敲门砖。 您有哪些证书呢？
		</div>
		<div class="certificate-edit" v-if="editState == 1">
			<div class="content">
				<el-form ref="form" :model="editForm" :rules="rules" label-width="85px" label-position="left" class="flex" hide-required-asterisk>
					<div class="edit-left flex-item">
						<div class="edit-item flex">
							<div class="red-dian"></div>
							<el-form-item label="证书名称：" class="flex-item" prop="certificateName">
								<el-input v-model="editForm.certificateName" placeholder="请输入证书名称"></el-input>
							</el-form-item>
						</div>
						
					</div>
					<div class="edit-right flex-item">
						<div class="edit-item flex">
							<div class="red-dian"></div>
							<el-form-item label="获得时间：" class="flex-item" prop="certificateTime">
								<el-date-picker
								v-model="editForm.certificateTime"
								type="month"
								value-format="yyyy-MM"
								placeholder="选择日期">
								</el-date-picker>
							</el-form-item>
						</div>
					</div>
				</el-form>
				<div class="form-option">
					<div class="save qida-success-btn" @click="saveEdit">保存</div>
					<div class="cancel qida-cancel-btn" @click="cancelEdit">取消</div>
					<span class="tips">添加你获得的专业技能、职业证书或职称；最多可以添加6份证书</span>
				</div>
			</div>
		</div>
	</div>
</template>
<script>
import {updateResumeCertificate} from '@/api/me/resume'
	export default {
		name:'certificate',
		props:['resumeCertificates'],
		data: function() {
			return {
				editState:0,//0:显示，1：编辑
				certificateIndex:0,
				form:{
					"certificateInfos":this.resumeCertificates,
					"resumeId": this.$route.query.id
				},
				rules: {
					certificateName: [{ required: true, trigger: "blur", message: "请输入证书名称" }],
					certificateTime: [{ required: true, trigger: "blur", message: "请选择获得时间" }],
				},
				editForm:{
					id: 0,
					certificateName:'',
					certificateTime:'',
					month: '',
					resumeId: this.$route.query.id,
					status: 1,
					year: ''
				}
			}
		},
		watch:{
			resumeCertificates(val){
				this.form.certificateInfos = val;
				this.form.resumeId=this.$route.query.id;
			}
		},
		methods: {
			saveEdit(){
				this.$refs.form.validate(valid => {
					if (valid) {
						this.editForm.year=this.editForm.certificateTime.split('-')[0];
						this.editForm.month=this.editForm.certificateTime.split('-')[1];
						updateResumeCertificate(this.editForm).then(res => {
							this.msgSuccess('保存成功');
							debugger
							this.editState=0;
							this.editForm.id = res.datas.id;
							if(this.editForm.index){
								this.form.certificateInfos[this.editForm.index] = this.editForm;
							}else{
								this.form.certificateInfos.push(this.editForm);
							}
						}).catch(err => {
							this.msgError(err);
						})
					}
				})
			},
			editEducation(index){
				this.certificateIndex=index;
				this.editForm={
					id: this.form.certificateInfos[index].id,
					certificateName: this.form.certificateInfos[index].certificateName,
					certificateTime: this.form.certificateInfos[index].year+'-'+this.form.certificateInfos[index].month,
					year: this.form.certificateInfos[index].year,
					month: this.form.certificateInfos[index].month,
					resumeId: this.$route.query.id,
					index: index
				}
				this.optionType=2;
				this.editState=1;
			},
			delOption(index){
				this.optionType=3;
				this.form.certificateInfos[index].resumeId=this.$route.query.id;
				this.form.certificateInfos[index].status = 0
				updateResumeCertificate(this.form.certificateInfos[index]).then(res => {
					if(res.resp_code == 0){
						this.msgSuccess('删除成功');
						this.form.certificateInfos.splice(index,1);
					}else{
						this.msgError(res.resp_msg);
					}
				}).catch(err => {
					this.$message.error(err);
				})
			},
			addOption(){
				this.optionType == 1;
				var arr={
					id: 0,
					schoolName:'',
					education:'',
					majorName:'',
					startTime:'',
					endTime:'',
					educationTime:['','']
				}
				this.form.certificateInfos.push(arr);
				this.certificateIndex=this.form.certificateInfos.length-1;
				this.editForm=arr;
				this.optionType = 1;
				this.editState = 1;
			},
			cancelEdit(){
				if(this.optionType == 1){
					this.form.certificateInfos.splice(this.certificateIndex,1);
				}
				this.editState = 0
			}
		}
	}
</script>
<style lang="scss">
	.certificate{
		width: 100%;
		position: relative;
		margin: 30px 0;
		.add-btn{
			color: #3EABC4;
			position: absolute;
			right: 0;
			top: 0;
			cursor: pointer;
			img{
				vertical-align: middle;
				margin-top: -2px;
				margin-right: 3px;
			}
		}
		.title{
			font-size: 16px;
			font-weight: bold;
			color: #3EABC4;
			border-left:4px solid #3EABC4;
			text-indent: 10px;
		}
		.certificate-content .no-data{
			padding-left: 10px;
			margin-top: 40px;
		}
		.certificate-content{
			margin-top: 25px;
			color: #787878;
			
			.certificate-item{
				min-height: 30px;
				line-height: 30px;
				.all-option{
					margin-left: 20px;
					opacity: 0;
					pointer-events: none;
					span{
						color: #3EABC4;
						margin-left: 10px;
						cursor: pointer;
					}
				}
				.label{
					padding-right: 20px;
					width: 156px;
					text-align: right;
				}
				.line{
					width: 7px;
					position: relative;
					.dian{
						width: 7px;
						height: 7px;
						background: #D0D0D0;
						border-radius: 50%;
						position: absolute;
						top: 11px;
						left: 0;
					}
					.line-box{
						border-right: 1px solid #D0D0D0;
						height: 100%;
						width: 1px;
						margin-left: 3px;
					}
				}
				.item-val{
					padding-left: 20px;
				}
			}
			.certificate-item:hover{
				.all-option{
					opacity: 1;
					pointer-events:painted;
				}
			}
			.certificate-item:first-child{
				.line{
					.line-box{
						border-right: 1px solid #D0D0D0;
						height: 50%;
						width: 1px;
						margin-left: 3px;
						position: absolute;
						top: 50%;
					}
				}
			}
			.certificate-item:last-child{
				.line{
					.line-box{
						border-right: 1px solid #D0D0D0;
						height: 50%;
						width: 1px;
						margin-left: 3px;
					}
				}
			}
		}
		.certificate-edit{
			.content{
				margin-top: 25px;
				padding-bottom: 20px;
				.edit-left{
					padding-right: 80px;
					
				}
				.edit-right{
					padding-right: 40px;
					
				}
				.edit-item{
					
					.el-form-item{
						margin-bottom: 20px;
						.el-form-item__label{
							font-weight: normal;
							color: #777777;
						}
						.el-input__inner{
							border-radius: 0px;
						}
						.select-input{
							width: 100%;
						}
						.el-input{
							width: 100%;
						}
						
					}
					.red-dian{
						width: 5px;
						height: 5px;
						border-radius: 2.5px;
						background-color: #d92032;
						margin-right: 5px;
						margin-top: 16px;
					}
					.red-dian.hide{
						opacity: 0;
					}
				}
				.form-option{
					margin-top: 10px;
					padding-left: 10px;
					.save{
						margin-right: 15px;
					}
					.tips{
						font-weight: 400;
						color: #BBBBBB;
						line-height: 36px;
						margin-left: 20px;
					}
					
				}
			}
		}
	}
</style>
